<template>
  <view class="uni-container">
    <uni-forms ref="form" class="layerPublicStyle" :model="formData" validateTrigger="bind">

      <uni-forms-item name="merchant_id" class="_margin_bottom10">
        <lay-form-item label="所属商户" class="_row_style">
          <div class="_content" v-if="formData.merchant_id==''"> </div>
          <div class="_content" v-else-if="Object.keys(merchantList).length>0">
            {{  merchantList.filter(it=>it._id==formData.merchant_id)[0].store_name }}
          </div>
            <!-- <lay-select class="_long_width_select" v-model="formData.merchant_id" :disabled="true" placeholder="请选择">
              <lay-select-option :value="it._id" 
                  :label="it.store_name"
                  v-for="(it,i) in merchantList"
                  :key="i"
              ></lay-select-option>
          </lay-select> -->
        </lay-form-item>
    </uni-forms-item>

      <uni-forms-item name="printer_sn" class="_margin_bottom10">
          <lay-form-item label="打印机编号SN"  class="_row_style">
            <div class="_content">
              {{  formData.printer_sn}}
            </div>
          </lay-form-item>
      </uni-forms-item>

    <uni-forms-item name="print_id" class="_margin_bottom10">
          <lay-form-item label="打印订单号"  class="_row_style">
            <div class="_content">
                    {{  formData.print_id || `-` }}
                </div>
          </lay-form-item>
      </uni-forms-item>

      <uni-forms-item name="print_content" class="_margin_bottom10">
          <lay-form-item label="打印内容"  class="_row_style">
            <lay-textarea  v-model="formData.print_content" style="width: 300px;" :disabled="true" :resize="true"></lay-textarea>
          </lay-form-item>
      </uni-forms-item>

      <uni-forms-item name="print_status" class="_margin_bottom10">
          <lay-form-item label="打印状态"  class="_row_style">
            <lay-radio 
              v-model="formData.print_status" 
              name="action" 
              :value="item.value" 
              :label="item.text"
              v-for="(item,index) in formOptions.print_status_localdata" :key="index"
            ></lay-radio>
          </lay-form-item>
      </uni-forms-item>
      <lay-form-item  class="_row_style" style="margin-top:30px">
        <lay-button @click="submit" type="normal">确认修改</lay-button>
        <lay-button @click="closeAll">取消关闭</lay-button>
      </lay-form-item>
    </uni-forms>
  </view>
</template>

<script>
  import { getAllMerchantList } from '@/utils/cloud.js'
  import { validator } from '../../js_sdk/validator/food-printer-order.js';

  const db = uniCloud.database();
  const dbCmd = db.command;
  const dbCollectionName = 'food-printer-order';

  function getValidator(fields) {
    let result = {}
    for (let key in validator) {
      if (fields.includes(key)) {
        result[key] = validator[key]
      }
    }
    return result
  }

  

  export default {
    data() {
      let formData = {
        "printer_id": "",
        "printer_sn": "",
        "merchant_id": "",
        "print_id": "",
        "print_content": "",
        "print_status": 0
      }
      return {
        merchantList:[],

        formData,
        formOptions: {
          "print_status_localdata": [
            {
              "text": "打印中",
              "value": 0
            },
            {
              "text": "已成功",
              "value": 1
            },
            {
              "text": "已失败",
              "value": 2
            }
          ]
        },
        rules: {
          ...getValidator(Object.keys(formData))
        }
      }
    },
    mounted() {
      this.$refs.form.setRules(this.rules)
      getAllMerchantList().then(({result:{data}})=>{
        this.merchantList=data
      })
    },
    methods: {
      init(id){
        console.log(id)
        this.formDataId = id
        this.getDetail(id)
      },
      closeAll(){
        this.$emit(`manualClose`,`addComponent`)
      },

      /**
       * 验证表单并提交
       */
      submit() {
        uni.showLoading({
          mask: true
        })
        this.$refs.form.validate().then((res) => {
          return this.submitForm(res)
        }).catch(() => {
        }).finally(() => {
          uni.hideLoading()
        })
      },

      /**
       * 提交表单
       */
      submitForm(value) {
        // 使用 clientDB 提交数据
        return db.collection(dbCollectionName).doc(this.formDataId).update(value).then((res) => {
          uni.showToast({
            title: '修改成功'
          })
          this.$emit(`refreshData`)
          this.closeAll();
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        })
      },

      /**
       * 获取表单数据
       * @param {Object} id
       */
      getDetail(id) {
        uni.showLoading({
          mask: true
        })
        db.collection(dbCollectionName).doc(id).field("printer_id,printer_sn,merchant_id,print_id,print_content,print_status").get().then((res) => {
          const data = res.result.data[0]
          if (data) {
            this.formData = data
            
          }
        }).catch((err) => {
          uni.showModal({
            content: err.message || '请求服务失败',
            showCancel: false
          })
        }).finally(() => {
          uni.hideLoading()
        })
      }
    }
  }
</script>
